* Discretion in Clinical Decision Making: Evidence from Bunching
* Claire Boone
* Encuesta de caracterización socioeconómica nacional (CASEN) 2015 
* https://observatorio.ministeriodesarrollosocial.gob.cl/encuesta-casen-2015

clear

global projdir    "`c(pwd)'"
global raw_data   "${projdir}/raw_data"
global gen_data   "${projdir}/gen_data"
global out     	  "${projdir}/out"

use "$raw_data/Casen 2015.dta"

* clean
	recode zona (1=0) (2=1), gen(rural)
	egen edad_mean     = mean(edad),     by(comuna)
	ta e1, gen(e1_)
	egen e1_1_mean     = mean(e1_1),     by(comuna)
	ta activ, gen(activ_)
	egen activ_1_mean  = mean(activ_1),  by(comuna)
	egen rural_mean    = mean(rural),    by(comuna)
	egen ypch_mean     = mean(ypch),     by(comuna)
	ta educ, gen(educ_)
	
	forval i=1/14 {
		egen educ_`i'_mean = mean(educ_`i'), by(comuna)
	}
	gen primary   = educ_1_mean + educ_2_mean + educ_3_mean
	gen secondary = educ_4_mean + educ_5_mean + educ_6_mean + educ_7_mean ///
				   + educ_8_mean + educ_9_mean
	gen tertiary  = educ_10_mean + educ_11_mean + educ_12_mean + educ_13_mean

	keep comuna edad_mean e1_1_mean activ_1_mean primary secondary tertiary rural_mean ypch_mean
	label drop comuna
	bysort comuna (comuna): keep if _n == 1
	
* save 
	export delimited using "${gen_data}/casen_micro.csv", replace
